package com.example.demo.offer;

import com.example.demo.lc.tree.LeetCode94;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class Offer44 {
    public static void main(String[] args) {
        //[1,3,2,5,3,null,9]
        TreeNode root  = new TreeNode(1);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode2  = new TreeNode(2);
        root.left = treeNode3;
        root.right = treeNode2;
        treeNode3.left = new TreeNode(5);
        treeNode3.right = new TreeNode(3);
        treeNode2.left = null;
        treeNode2.right = new TreeNode(9);
        System.out.println(largestValues(root));
    }


    public static List<Integer> largestValues(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Deque<TreeNode> queue = new LinkedList<>();
        if(root == null){
            return res;
        }
        queue.offer(root);
        while (!queue.isEmpty()){
            int lenth = queue.size();
            int ans = Integer.MIN_VALUE;
            while (lenth > 0){
                TreeNode poll = queue.poll();
                ans = Math.max(ans,poll.val);
                if(poll.left != null){
                    queue.offer(poll.left);
                }
                if(poll.right != null){
                    queue.offer(poll.right);
                }
                lenth--;
            }
            res.add(ans);
        }
        return res;
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
